package com.heima.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.pojo.dto.Node;
import com.heima.mapper.NodeMapper;
import com.heima.pojo.dto.NodeDto;
import com.heima.pojo.dto.NodeQuery;
import com.heima.pojo.vo.NodeVo;
import com.heima.pojo.vo.PageVo;
import com.heima.service.INodeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 点位表 服务实现类
 * </p>
 *
 * @author 存折
 * @since 2024-10-19
 */
@Service
public class NodeServiceImpl extends ServiceImpl<NodeMapper, Node> implements INodeService {

    @Autowired
    private NodeMapper nodeMapper;
    @Override
    public PageVo<NodeVo> query(NodeQuery nodeQuery) {
        String name = nodeQuery.getName();
        String regionId = nodeQuery.getRegionId();
        Page page = new Page<>(nodeQuery.getPageIndex(), nodeQuery.getPageSize());
        Page<Node> p = lambdaQuery().
                like(name != null, Node::getName, name)
                .eq(regionId != null, Node::getRegionId, regionId)
                .page(page);
        long pages = p.getPages();
        long total = p.getTotal();
        List<Node> records = p.getRecords();
        List<NodeVo> nodeVoList = new ArrayList<>();
        records.forEach(node -> {
            NodeVo nodeVo = new NodeVo();
            BeanUtils.copyProperties(node,nodeVo);
            nodeVo.setVmCount(nodeMapper.getcount(node.getId()));
            nodeVo.setBusinessType(nodeMapper.getBusName(node.getBusinessId()));
            nodeVo.setRegion(nodeMapper.getRegName(node.getRegionId()));
            nodeVoList.add(nodeVo);
        });
return new PageVo<NodeVo>(nodeQuery.getPageIndex(), nodeQuery.getPageSize(),pages,total,nodeVoList);
    }

    @Override
    public List<NodeDto> selectDetail(Long id) {
      List<NodeDto> list=nodeMapper.selectDetail(id);
       return list;
    }
}
